<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>购买通行证</title>
  </head>
  <body>
    <script>
      // https://www.lintcode.com/problem/1851/
      function buyPasses(arr, k) {
        // Write your code here.
        for (let i = 0; i < arr.length; i++) {
          if (arr[i] - 1 > 0) {
            let a = arr[i] - 1;
            arr.push(a);
            if (i === k) {
              k = arr.length - 1;
            }
          } else if (arr[i] - 1 === 0) {
            if (i === k) {
              return i + 1;
            }
          }
        }
      }

      let arr = [1, 2, 5],
        k = 1;
      console.log(buyPasses(arr, k));
      // 问题：消耗的内存量太大
    </script>
  </body>
</html>
